package com.ypz.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ypz.reggie.common.MyBaseContext;
import com.ypz.reggie.common.R;
import com.ypz.reggie.entity.ShoppingCart;
import com.ypz.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

        //设置当前用户id
        Long currentId = MyBaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //判断是套餐还是菜品，并判断是不是第一次加入
        Long setmealId = shoppingCart.getSetmealId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper();
        lqw.eq(ShoppingCart::getUserId , currentId);
        lqw.orderByDesc(ShoppingCart::getCreateTime);

        if (setmealId != null){
            //套餐id
            lqw.eq(ShoppingCart::getSetmealId,setmealId);
            ShoppingCart one = shoppingCartService.getOne(lqw);

            if (one == null){
                //第一次加入套餐
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());

                shoppingCartService.save(shoppingCart);
            } else {
                //不是第一次加入，更新数据
                Integer number = one.getNumber();
                one.setNumber(number + 1);
                shoppingCartService.update(one,lqw);

                //便于返回给页面
                shoppingCart = one;
            }

        } else{
            //菜品id
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
            ShoppingCart one = shoppingCartService.getOne(lqw);

            if (one == null){
                //第一次加入菜品
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());

                shoppingCartService.save(shoppingCart);
            } else {
                //不是第一次加入，更新数据
                Integer number = one.getNumber();
                one.setNumber(number + 1);
                shoppingCartService.update(one,lqw);

                //便于返回给页面
                shoppingCart = one;
            }

        }

        return R.success(shoppingCart);
    }

    /**
     * 根据用户id查购物车，用户id在拦截器里已经设置了
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        Long currentId = MyBaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);
        lqw.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(lqw);

        return R.success(list);

    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> delete(){

        Long currentId = MyBaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);

        shoppingCartService.remove(lqw);

        return R.success("清楚购物车成功");

    }


}
